const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 3000

// 解析 application/json
app.use(bodyParser.json())
// 解析 application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// 允许跨域
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*')
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
  res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
  if (req.method === 'OPTIONS') {
    return res.sendStatus(200)
  }
  next()
})

// 登录接口
app.post('/api/user/login', (req, res) => {
  res.json({
    code: 200,
    data: {
      token: 'mock-token',
      user: {
        id: 1,
        username: 'admin',
        name: '管理员',
        avatar: 'https://via.placeholder.com/100'
      }
    },
    message: '登录成功'
  })
})

// 用户信息接口
app.get('/api/user/info', (req, res) => {
  res.json({
    code: 200,
    data: {
      id: 1,
      username: 'admin',
      name: '管理员',
      avatar: 'https://via.placeholder.com/100',
      roles: ['admin']
    },
    message: '获取成功'
  })
})

// 商品列表接口
app.get('/api/shop/product/list', (req, res) => {
  const mockData = Array.from({ length: 20 }).map((_, index) => ({
    id: index + 1,
    name: `商品${index + 1}`,
    category: ['中药材', '中药饮片', '中成药', '保健品', '养生茶', '养生器具'][index % 6],
    image: `https://via.placeholder.com/100?text=商品${index + 1}`,
    price: (Math.random() * 500 + 50).toFixed(2),
    originalPrice: (Math.random() * 700 + 100).toFixed(2),
    stock: Math.floor(Math.random() * 1000),
    sales: Math.floor(Math.random() * 500),
    status: Math.random() > 0.3 ? 'on' : 'off',
    brief: `商品${index + 1}简介`,
    detail: `商品${index + 1}详细描述`,
    createTime: '2023-05-01 10:00:00',
    updateTime: '2023-05-10 15:30:00',
    specs: [
      {
        name: '规格1',
        price: (Math.random() * 500 + 50).toFixed(2),
        stock: Math.floor(Math.random() * 100)
      },
      {
        name: '规格2',
        price: (Math.random() * 500 + 50).toFixed(2),
        stock: Math.floor(Math.random() * 100)
      }
    ]
  }))
  
  res.json({
    code: 200,
    data: {
      list: mockData,
      total: 100
    },
    message: '获取成功'
  })
})

// 商品详情接口
app.get('/api/shop/product/:id', (req, res) => {
  const id = req.params.id
  res.json({
    code: 200,
    data: {
      id,
      name: `商品${id}`,
      category: '中药材',
      image: `https://via.placeholder.com/300?text=商品${id}`,
      images: [
        `https://via.placeholder.com/300?text=商品${id}-1`,
        `https://via.placeholder.com/300?text=商品${id}-2`,
        `https://via.placeholder.com/300?text=商品${id}-3`
      ],
      price: 199.00,
      originalPrice: 299.00,
      stock: 500,
      sales: 200,
      status: 'on',
      brief: `这是商品${id}的简介，描述了这个商品的基本特点和用途。`,
      detail: `这是商品${id}的详细描述，包含了产品的功效、用法用量、注意事项等详细信息。这里可以包含很多专业的描述，帮助用户了解商品的各种特性和优势。`,
      specs: [
        { name: '规格1', price: 199.00, stock: 200 },
        { name: '规格2', price: 249.00, stock: 150 },
        { name: '规格3', price: 299.00, stock: 100 }
      ],
      createTime: '2023-05-01 10:00:00',
      updateTime: '2023-05-10 15:30:00'
    },
    message: '获取成功'
  })
})

// 启动服务
app.listen(port, () => {
  console.log(`Mock server running at http://localhost:${port}`)
}) 